package com.tgy.队列;

import java.util.Scanner;

/***
 * @ClassName: CircleQueueDemo
 * @Description: 使用数组模拟环形队列
 * @Auther: tanggy3
 * @Date: 15:36 2022/5/17
 * @version : V1.0
 */
public class CircleQueueDemo {
    public static void main(String[] args) {
//测试一把
        System.out.println("测试数组模拟环形队列的案例~~~");

        // 创建一个环形队列
        CircleQueue queue = new CircleQueue(4); //说明设置4, 其队列的有效数据最大是3
        char key = ' '; // 接收用户输入
        Scanner scanner = new Scanner(System.in);//
        boolean loop = true;
        // 输出一个菜单
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);// 接收一个字符
            switch (key) {
                case 's':
                    queue.show2();
                    break;
                case 'a':
                    System.out.println("输入一个数");
                    int value = scanner.nextInt();
                    queue.addElement(value);
                    break;
                case 'g': // 取出数据
                    try {
                        int res = queue.getElement();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': // 查看队列头的数据
                    try {
                        int res = queue.getFront();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': // 退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~");
    }
}

class CircleQueue{

    public static void main(String[] args) {
        CircleQueue circleQueue = new CircleQueue(5);
        circleQueue.addElement(1);
        circleQueue.addElement(2);
        System.out.println(circleQueue.size());
        System.out.println(circleQueue.getRear());
        circleQueue.show();
    }
    private int maxSize;//数组最大容量
    private int front;//头指针，指向当前元素
    private int rear;//尾指针，指向当前元素的后一个
    private int[] arr;//数组

    public CircleQueue(int maxSize){
        this.maxSize = maxSize;
        this.front = 0;
        this.rear = 0;
        arr = new int[maxSize];
    }

    //Queen is full?
    public boolean isFull(){
        return (rear+1)%maxSize == front;
    }
    //Queen is empty?
    public boolean isEmpty(){
        return rear == front;
    }
    //队列中元素个数
    public int size(){
        return (rear-front+maxSize)%maxSize;
    }
    //入队
    public void addElement(int value){
        if(isFull()){
            System.out.println("队列已满");
            return;
        }
        arr[rear] = value;//将值放入
        rear = (rear+1)%maxSize;//将尾指针后移，注意取模
    }
    //出队
    public int getElement(){
        if(isEmpty()){
            System.out.println("队列为空");
            return -1;
        }
        int value = arr[front];
        front = (front+1)%maxSize;//头指针后移，环形队列记得取模
        return value;
    }
    //查看队首元素
    public int getFront(){
        if(isEmpty()){
            System.out.println("队列为空");
            return -1;
        }
        return arr[front];
    }
    //查看队尾元素
    public int getRear(){
        if(isEmpty()){
            System.out.println("队列为空");
            return -1;
        }
        return arr[rear-1];
    }
    //打印队列
    public void show(){
        if(isEmpty()){
            System.out.println("队列为空");
            return;
        }
        int i = front;
        while (i != rear-1){
            System.out.printf("arr[%d]=%d",i,arr[i]);
            i = (i+1)%maxSize;
        }
        System.out.println();
    }

    //打印队列
    public void show2(){
        if(isEmpty()){
            System.out.println("队列为空");
            return;
        }
        int size = size();
        for (int i = front; i <front+size; i++) {
            System.out.printf("arr[%d]=%d\n",i%maxSize,arr[i%maxSize]);
        }
    }
}





